package com.ukao.steward.dialog;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseDialogFragment;
import com.ukao.steward.util.CheckUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.ukao.steward.consts.Constant.ARG_PARAM3;


public class BatchDialogFragment extends BaseDialogFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    Unbinder unbinder;
    @BindView(R.id.succeed_image)
    ImageView succeedImage;
    @BindView(R.id.error_msg)
    TextView errorMsg;
    @BindView(R.id.layout)
    LinearLayout layout;
    @BindView(R.id.batch_warning_image)
    ImageView batchWarningImage;

    private String mParam1;
    private String mParam2;
    private int type;
    private boolean isReceiveSucceed;
    private String errorMsgParam;
    private int longDismiss;


    public static BatchDialogFragment newInstance(boolean isReceiveSucceed, int longDismiss, String errorMsg) {
        BatchDialogFragment fragment = new BatchDialogFragment();
        Bundle args = new Bundle();
        args.putBoolean(ARG_PARAM1, isReceiveSucceed);
        args.putInt(ARG_PARAM2, longDismiss);
        args.putString(ARG_PARAM3, errorMsg);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            isReceiveSucceed = getArguments().getBoolean(ARG_PARAM1, false);
            longDismiss = getArguments().getInt(ARG_PARAM2);
            errorMsgParam = getArguments().getString(ARG_PARAM3);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }


    @Override
    protected void initView() {

        if (isReceiveSucceed) {
            succeedImage.setVisibility(View.VISIBLE);
            batchWarningImage.setVisibility(View.GONE);
            errorMsg.setVisibility(View.GONE);
        } else {
            layout.setBackgroundResource(R.drawable.shape_receive_state_pink);
            errorMsg.setText(CheckUtils.isEmptyString(errorMsgParam));
        }



    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }

    public static Builder createBuilder(
            FragmentManager fragmentManager) {
        return new Builder(fragmentManager);
    }
    public static final class Builder {
        private boolean isReceiveSucceed;
        private String errorMsg;
        private FragmentManager fragmentManager;
        private int longDismiss;



        public Builder(FragmentManager fragmentManager) {
            this.fragmentManager = fragmentManager;
        }

        public Builder isReceiveSucceed(boolean val) {
            isReceiveSucceed = val;
            return this;
        }

        public Builder errorMsg(String val) {
            errorMsg = val;
            return this;
        }


        public BatchDialogFragment show() {


            BatchDialogFragment mUnTakeDialog = BatchDialogFragment.newInstance(isReceiveSucceed,longDismiss,errorMsg);
            mUnTakeDialog.show(fragmentManager, mUnTakeDialog.getClass().getName());
            return mUnTakeDialog;
        }
    }
}
